﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace MulticastTV
{
    class PasswordEncription
    {

        private static bool unlocked = false;
        private static string _password = "";
        private static byte[] Entropy = { 3, 2, 1, 4, 6, 5 };

        public static string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        public static bool Unlocked
        {
            get { return unlocked; }
            set { unlocked = value; }
        }

        public static void decrypt()
        {

            if (Properties.Settings.Default.Password.Length > 0)
            {

                byte[] enc_password = Convert.FromBase64String(Properties.Settings.Default.Password);

                byte[] dec_password = ProtectedData.Unprotect(enc_password, Entropy, DataProtectionScope.LocalMachine);

                _password = Encoding.Unicode.GetString(dec_password);
            }
            else
            {
                _password = "";
            }
        }

        public static void encrypt()
        {
            if (PasswordEncription.Password.Length > 0)
            {
                // encrypt password and username
                UnicodeEncoding uni = new UnicodeEncoding();

                byte[] password = uni.GetBytes(PasswordEncription.Password);

                byte[] enc_password = ProtectedData.Protect(password, Entropy, DataProtectionScope.LocalMachine);

                Properties.Settings.Default.Password = Convert.ToBase64String(enc_password);
            }
            else
            {
                Properties.Settings.Default.Password = "";
            }
        }
       
    }
}
